package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.dto.GuaranteeCaseDTO;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.service.ILoginService;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.customer.domain.GuaranteeCase;
import com.ktgj.customer.service.IGuaranteeCaseService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 工单Controller
 * 
 * @author ruoyi
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/case")
public class CaseController extends BaseController
{
    @Autowired
    private IGuaranteeCaseService guaranteeCaseService;

    @Autowired
    private ILoginService loginService;



    /**
     * 查询工单列表
     */
    //@PreAuthorize(hasPermi = "customer:case:list")
    @GetMapping("/list")
    public TableDataInfo list(GuaranteeCase guaranteeCase)
    {
        startPage();
        List<GuaranteeCase> list = guaranteeCaseService.selectGuaranteeCaseList(guaranteeCase);
        return getDataTable(list);
    }

    /**
     * 导出工单列表
     */
    //@PreAuthorize(hasPermi = "customer:case:export")
    @Log(title = "工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GuaranteeCase guaranteeCase) throws IOException
    {
        List<GuaranteeCase> list = guaranteeCaseService.selectGuaranteeCaseList(guaranteeCase);
        ExcelUtil<GuaranteeCase> util = new ExcelUtil<GuaranteeCase>(GuaranteeCase.class);
        util.exportExcel(response, list, "工单数据");
    }

    /**
     * 获取工单详细信息
     */
    //@PreAuthorize(hasPermi = "customer:case:query")
    @GetMapping(value = "/{caseId}")
    public AjaxResult getInfo(@PathVariable("caseId") Long caseId)
    {
        return AjaxResult.success(guaranteeCaseService.selectGuaranteeCaseById(caseId));
    }

    /**
     * 新增工单
     */
    //@PreAuthorize(hasPermi = "customer:case:add")
    @Log(title = "工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GuaranteeCase guaranteeCase)
    {
        return toAjax(guaranteeCaseService.insertGuaranteeCase(guaranteeCase));
    }

    /**
     * 修改工单
     */
    //@PreAuthorize(hasPermi = "customer:case:edit")
//    @Log(title = "工单", businessType = BusinessType.UPDATE)
    @PostMapping("/editCase")
    public AjaxResult edit(@RequestBody GuaranteeCase guaranteeCase)
    {
        return toAjax(guaranteeCaseService.updateGuaranteeCase(guaranteeCase));
    }

    /**
     * 删除工单
     */
    //@PreAuthorize(hasPermi = "customer:case:remove")
    @Log(title = "工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{caseIds}")
    public AjaxResult remove(@PathVariable Long[] caseIds)
    {
        return toAjax(guaranteeCaseService.deleteGuaranteeCaseByIds(caseIds));
    }

    /**
     * 查询工单
     * @param guaranteeCase
     * @return
     */
    @PostMapping("/selectGuaranteeCase")
    public AjaxResult selectGuaranteeCase(@RequestBody GuaranteeCaseVO guaranteeCase) {
        List<GuaranteeCaseDTO> caseDTO = guaranteeCaseService.selectGuaranteeCase(guaranteeCase);
        if (caseDTO != null && caseDTO.size() > 0) {
            return AjaxResult.success(caseDTO);
        }
        return AjaxResult.error("查询失败！");
    }

    /**
     * 创建工单
     * @param request
     * @param guaranteeCaseAdd
     * @return
     */
    @PostMapping("/addGuaranteeCase")
    public AjaxResult addGuaranteeCase(HttpServletRequest request, @RequestBody GuaranteeCaseAddVO guaranteeCaseAdd) {
        boolean guaranteeCase = guaranteeCaseService.addGuaranteeCase(request, guaranteeCaseAdd);
        if (guaranteeCase)
            return AjaxResult.success("创建成功！");
        return AjaxResult.error("创建失败！");
    }

    /**
     * 后端——查询工单
     * */
    @PostMapping("/backSelectGuaranteeCase")
    public AjaxResult backSelectGuaranteeCase(HttpServletRequest request){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        List<TemporaryGuaranteeCase> temporaryGuaranteeCaseList;
        try {
            temporaryGuaranteeCaseList = guaranteeCaseService.backSelectGuaranteeCase(customerId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (temporaryGuaranteeCaseList != null && temporaryGuaranteeCaseList.size() > 0){
            return AjaxResult.success(temporaryGuaranteeCaseList);
        }
        else {
            return AjaxResult.error("未查到数据");
        }
    }

    /**
     * 后端——条件查询工单
     * */
    @PostMapping("/backGuaranteeCaseById")
    public TableDataInfo backSelectGuaranteeCaseById(@RequestBody GuaranteeCaseVO guaranteeCase){
        startPage();
        List<GuaranteeCase> guaranteeCases = guaranteeCaseService.backSelectGuaranteeCaseById(guaranteeCase);
        return getDataTable(guaranteeCases);
}

    /**
     * 后端——创建工单
     * */
    @PostMapping("/backGuaranteeCase")
    public AjaxResult backInsertGuaranteeCase(HttpServletRequest request, @RequestBody GuaranteeCaseParameter guaranteeCaseParameter){
        if (guaranteeCaseParameter != null){
            int i;
            try {
                if (guaranteeCaseParameter.getType() == null || guaranteeCaseParameter.getType().equals("")){
                    return AjaxResult.error("传入类型不能为空");
                }
                if (guaranteeCaseParameter.getType().equals("Crm")){
                    i = guaranteeCaseService.backInsertGuaranteeCase(request, guaranteeCaseParameter);
                }
                else {
                    i = guaranteeCaseService.insertGuaranteeCase(request, guaranteeCaseParameter);
                }
            } catch (Exception e){
                e.printStackTrace();
                return AjaxResult.error(e.getMessage());
            }

            if (i > 0){
                return AjaxResult.success("新增成功");
            }
            else {
                return AjaxResult.error("新增失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——查询工单详细
     * */
    @PostMapping("/backGuaranteeById")
    public AjaxResult backGuaranteeCaseById(@RequestBody Map<String, Object> map){
        if (map.get("caseId") != null && !map.get("caseId").equals("")){
            Long caseId = Long.valueOf(map.get("caseId").toString());
            GuaranteeCaseParameterVo guaranteeCaseParameterVo;
            try {
                guaranteeCaseParameterVo = guaranteeCaseService.backGuaranteeCaseById(caseId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (guaranteeCaseParameterVo != null){
                return AjaxResult.success(guaranteeCaseParameterVo);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
    /**
     * 后端——工单小类列表
     * */
    @PostMapping("/selectworkOrderType")
    public AjaxResult selectworkOrderType(@RequestBody Map<String, Object> map){
        Long deptId = null;
        if (map.get("caseTypeId") != null && !map.get("caseTypeId").equals("")){
            deptId = Long.valueOf(map.get("caseTypeId").toString());
        }
        List<TmpGuaranteeCaseVo> tmpGuaranteeCaseVos = guaranteeCaseService.selectworkOrderType(deptId);
        return AjaxResult.success(tmpGuaranteeCaseVos);
    }

    /**
     * 查询待修改的工单信息
     * */
    @PostMapping("/guaranteeCaseParameter")
    public AjaxResult guaranteeCaseParameter(@RequestBody Map<String, Object> map){
        if (map.get("caseId") != null && !map.get("caseId").equals("")){
            Long caseId = Long.valueOf(map.get("caseId").toString());
            GuaranteeCaseParameter guaranteeCaseParameter = guaranteeCaseService.guaranteeCaseParameter(caseId);
            return AjaxResult.success(guaranteeCaseParameter);
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
